public class TestDemo {
    //创建两把锁
    static Object lock1 = new Object();
    static Object lock2 = new Object();

    public static void main(String[] args) {
        Thread thread1 = new Thread(){
            @Override
            public void run() {
                synchronized(lock1){
                    System.out.println("线程1获得lock1");
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程1尝试获取lock2...");
                    synchronized(lock2){
                        System.out.println("线程1获得了lock2");
                    }
                }
            }
        };
        thread1.start();

        Thread thread2 = new Thread(){
            @Override
            public void run() {
                synchronized(lock2){
                    System.out.println("线程2获得lock2");
                    try {
                        sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程2尝试获取lock1...");
                    synchronized(lock1){
                        System.out.println("线程2获得了lock1");
                    }
                }
            }
        };
        thread2.start();
    }
}
